#include<iostream>
#include"Socket.hpp"
#include"log.hpp"
#include<memory>
#include"Protocol.hpp"

void Handle(int* x,int* y,char* oper)
{
    std::cout<<"Please Enter x:"<<std::endl;
    std::cin>> *x;
    std::cout<<"Please Enter y:"<<std::endl;
    std::cin>> *y;
    std::cout<<"Please Enter oper:"<<std::endl;
    std::cin>> *oper;
}
using namespace LogModule;
int main(int argc,char* argv[])
{
    if (argc != 3)
    {
        std::cout << "Usage:" << argv[0] << " +serveip"<<" +serveport";
        return 1;
    }

    std::string server_ip=argv[1];
    uint16_t server_port=std::stoi(argv[2]);

    std::unique_ptr<Protocol> protocol=std::make_unique<Protocol>();

    std::shared_ptr<Socket> CLI=std::make_shared<TCPSocket>();
    CLI->Build_client_TCPSocket();
    int ret=CLI->Connect(server_ip,server_port);
    if(ret==0)
    {
        LOG(LogLevel::INFO)<<"connect success";
        //连接成功后我们需要从标准输入中获取x,y并构建请求发给服务器
        std::string inbuffer;
        while(1)
        {
            int x,y;
            char oper;
            Handle(&x,&y,&oper);
            std::string req_str=protocol->BuildRequest(x,y,oper);
            CLI->Send(req_str);

            //发送完接受并处理服务器发送过来的报文和结果：
            //主要是协议层帮我们处理：
            Response resp;
            bool ret=protocol->GetResponse(CLI,&inbuffer,&resp);
            if(ret==false)
            {
                break;
            }
            resp.ShowResult();
        }
    }
    else
    {
        LOG(LogLevel::FATAL)<<"connect fail!!";
    }
    CLI->Close();
    return 0;
}